package ch02.jianDa;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class StudentInfo2 {
    public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, TransformerException, DocumentException {
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read(new File("src/ch02/jianDa/Student.xml"));
        Element rootElement = doc.getRootElement();
        List<Element> student1 = rootElement.elements("Student");
        for (Element element : student1) {
            System.out.println("学号："+element.attribute("id").getText());
            String name = element.element("name").getText();
            System.out.println("\t姓名："+name);
            System.out.println("\t科目："+element.element("course").getText());
            System.out.println("\t分数："+element.element("score").getText());
        }
        OutputFormat compactFormat = OutputFormat.createCompactFormat();
        compactFormat.setEncoding("UTF-8");
        XMLWriter xmlWriter = new XMLWriter(new FileWriter("src/ch02/jianDa/Student.xml"),compactFormat);
        xmlWriter.write(doc);
        xmlWriter.close();
    }
}
